import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import matplotlib
from matplotlib import font_manager, rc
import platform
if platform.system()=="Windows":
font_name=font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)
matplotlib.rcParams['axes.unicode_minus']=False
import warnings
warnings.filterwarnings("ignore")
# 데이터셋 호출
diab = pd.read_csv("./Downloads/diabetes.csv")
diab.head()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | 50 | 1 |
| 1 | 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | 31 | 0 |
| 2 | 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | 32 | 1 |
| 3 | 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | 0 |
| 4 | 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | 33 | 1 |
# 데이터셋 정보 확인
diab.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 768 entries, 0 to 767 Data columns (total 9 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Pregnancies 768 non-null int64 1 Glucose 768 non-null int64 2 BloodPressure 768 non-null int64 3 SkinThickness 768 non-null int64 4 Insulin 768 non-null int64 5 BMI 768 non-null float64 6 DiabetesPedigreeFunction 768 non-null float64 7 Age 768 non-null int64 8 Outcome 768 non-null int64 dtypes: float64(2), int64(7) memory usage: 54.1 KB
# Glucose : 혈중 포도당
# DiabetesPedigreeFunction : 가족 이력
# 데이터셋의 결측치 확인
diab.isna().sum()
Pregnancies 0 Glucose 0 BloodPressure 0 SkinThickness 0 Insulin 0 BMI 0 DiabetesPedigreeFunction 0 Age 0 Outcome 0 dtype: int64
NaN 값은 존재하지 않는걸 확인할 수 있다.
diab.describe().T
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| Pregnancies | 768.0 | 3.845052 | 3.369578 | 0.000 | 1.00000 | 3.0000 | 6.00000 | 17.00 |
| Glucose | 768.0 | 120.894531 | 31.972618 | 0.000 | 99.00000 | 117.0000 | 140.25000 | 199.00 |
| BloodPressure | 768.0 | 69.105469 | 19.355807 | 0.000 | 62.00000 | 72.0000 | 80.00000 | 122.00 |
| SkinThickness | 768.0 | 20.536458 | 15.952218 | 0.000 | 0.00000 | 23.0000 | 32.00000 | 99.00 |
| Insulin | 768.0 | 79.799479 | 115.244002 | 0.000 | 0.00000 | 30.5000 | 127.25000 | 846.00 |
| BMI | 768.0 | 31.992578 | 7.884160 | 0.000 | 27.30000 | 32.0000 | 36.60000 | 67.10 |
| DiabetesPedigreeFunction | 768.0 | 0.471876 | 0.331329 | 0.078 | 0.24375 | 0.3725 | 0.62625 | 2.42 |
| Age | 768.0 | 33.240885 | 11.760232 | 21.000 | 24.00000 | 29.0000 | 41.00000 | 81.00 |
| Outcome | 768.0 | 0.348958 | 0.476951 | 0.000 | 0.00000 | 0.0000 | 1.00000 | 1.00 |
최소값이 0인 데이터들이 많이 보인다.
그런데 Glucose, BloodPressure, SkinThickness, Insulin, BMI의 경우는 0일 수가 없다.
그래서 이 컬럼들의 값이 0인경우를 결측치로 생각해야한다.
결측치로 처리해야할 컬럼(값이 0인 경우)
Glucose, BloodPressure, SkinThickness, Insulin, BMI
# 결측치가 있는 컬럼 찾기
missingValue=[]
for key in diab.keys()[1:-1]:
if True in (diab[key]==0).values:
missingValue.append(key)
# 결측치를 NaN으로 처리
for col in missingValue:
cnt=0
for j in diab[col].values:
if j == 0:
diab[col][cnt]=np.nan
cnt+=1
diab.isna().sum()
Pregnancies 0 Glucose 5 BloodPressure 35 SkinThickness 227 Insulin 374 BMI 11 DiabetesPedigreeFunction 0 Age 0 Outcome 0 dtype: int64
# 결측치가 나온 컬럼들과 결측치 수 그래프
isNa = diab.isna().sum().to_frame().T
# isNa
x = isNa.columns
y = isNa.values[0]
plt.figure(figsize=(15,8))
plt.bar(x,y)
plt.title("결측치 개수",fontdict={"size":20})
Text(0.5, 1.0, '결측치 개수')
# diab.describe()
diab.hist(figsize = (20,20))
array([[<AxesSubplot:title={'center':'Pregnancies'}>,
<AxesSubplot:title={'center':'Glucose'}>,
<AxesSubplot:title={'center':'BloodPressure'}>],
[<AxesSubplot:title={'center':'SkinThickness'}>,
<AxesSubplot:title={'center':'Insulin'}>,
<AxesSubplot:title={'center':'BMI'}>],
[<AxesSubplot:title={'center':'DiabetesPedigreeFunction'}>,
<AxesSubplot:title={'center':'Age'}>,
<AxesSubplot:title={'center':'Outcome'}>]], dtype=object)
0인 값을 결측치로 처리하기에 피부두께와 인슐린은 결측치로 변화되는 값이 너무 많다
피부두께와 인슐린의 NaN값을 모두 지운다면 데이터의 수가 너무 줄어들것 같다.
그런데 피부두께와 인슐린을 제외하고는 NaN값이 존재하는 행을 지워도 데이터의 수가 크게 줄어들진 않으므로
나머지 컬럼의 NaN값을 갖는 행은 제거하고 피부두께와 인슐린의 결측치는 중위값으로 대체하자
# "Glucose","BloodPressure","BMI" 컬럼들의 NaN값이 있는 행 제거
diab.dropna(subset=["Glucose","BloodPressure","BMI"],inplace=True)
인슐린과 피부두께의 결측치는 당뇨병 여부의 평균으로 넣어주자
diab.fillna({"Insulin":diab["Insulin"].median(),
"SkinThickness":diab["SkinThickness"].median()},inplace=True)
# diab.isna().sum()
diab.corr()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
|---|---|---|---|---|---|---|---|---|---|
| Pregnancies | 1.000000 | 0.134915 | 0.209668 | 0.078710 | 0.023269 | 0.012342 | -0.025996 | 0.557066 | 0.224417 |
| Glucose | 0.134915 | 1.000000 | 0.223331 | 0.195340 | 0.427117 | 0.223276 | 0.136630 | 0.263560 | 0.488384 |
| BloodPressure | 0.209668 | 0.223331 | 1.000000 | 0.191570 | 0.045832 | 0.287403 | -0.000075 | 0.324897 | 0.166703 |
| SkinThickness | 0.078710 | 0.195340 | 0.191570 | 1.000000 | 0.154145 | 0.555098 | 0.106670 | 0.124268 | 0.216357 |
| Insulin | 0.023269 | 0.427117 | 0.045832 | 0.154145 | 1.000000 | 0.184102 | 0.129494 | 0.096894 | 0.211736 |
| BMI | 0.012342 | 0.223276 | 0.287403 | 0.555098 | 0.184102 | 1.000000 | 0.154858 | 0.020835 | 0.299375 |
| DiabetesPedigreeFunction | -0.025996 | 0.136630 | -0.000075 | 0.106670 | 0.129494 | 0.154858 | 1.000000 | 0.023098 | 0.184947 |
| Age | 0.557066 | 0.263560 | 0.324897 | 0.124268 | 0.096894 | 0.020835 | 0.023098 | 1.000000 | 0.245741 |
| Outcome | 0.224417 | 0.488384 | 0.166703 | 0.216357 | 0.211736 | 0.299375 | 0.184947 | 0.245741 | 1.000000 |
diab.describe()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
|---|---|---|---|---|---|---|---|---|---|
| count | 724.000000 | 724.000000 | 724.000000 | 724.000000 | 724.000000 | 724.000000 | 724.000000 | 724.000000 | 724.000000 |
| mean | 3.866022 | 121.882597 | 72.400552 | 29.133978 | 142.044199 | 32.467127 | 0.474765 | 33.350829 | 0.343923 |
| std | 3.362803 | 30.750030 | 12.379870 | 9.019267 | 88.713438 | 6.888941 | 0.332315 | 11.765393 | 0.475344 |
| min | 0.000000 | 44.000000 | 24.000000 | 7.000000 | 14.000000 | 18.200000 | 0.078000 | 21.000000 | 0.000000 |
| 25% | 1.000000 | 99.750000 | 64.000000 | 25.000000 | 118.250000 | 27.500000 | 0.245000 | 24.000000 | 0.000000 |
| 50% | 3.000000 | 117.000000 | 72.000000 | 29.000000 | 125.500000 | 32.400000 | 0.379000 | 29.000000 | 0.000000 |
| 75% | 6.000000 | 142.000000 | 80.000000 | 33.000000 | 130.500000 | 36.600000 | 0.627500 | 41.000000 | 1.000000 |
| max | 17.000000 | 199.000000 | 122.000000 | 99.000000 | 846.000000 | 67.100000 | 2.420000 | 81.000000 | 1.000000 |
BMI지수와 피부두께, 인슐린분비와 포도당, 나이와 임신횟수의 상관관계가 높게 나타난다.
plt.figure(figsize=(20,14))
corr = diab.corr()
mask = np.triu(np.ones_like(corr, dtype=bool))
sns.heatmap(corr, annot=True, fmt = ".2f", lw = 0.5, mask=mask)
<AxesSubplot:>
# 당뇨병 여부에 따른 컬럼들의 평균치
outcome = diab.groupby("Outcome").mean()
# outcome.T
outcome.T.plot.bar(figsize=(12,7), rot=0)
<AxesSubplot:>
# 당뇨병 환자의 비율
out = diab["Outcome"].value_counts().to_frame()
# out
px.pie(out,values="Outcome",names="Outcome")
당뇨병 환자의 연령별 분포
plt.figure(figsize=(12,5))
sns.histplot(diab[diab["Outcome"]==1]["Age"],
bins = 10)
<AxesSubplot:xlabel='Age', ylabel='Count'>
당뇨병 환자는 20대 ~ 40대 중반이 가장 많은걸로 보인다
그런데 50대, 60대에서는 왜 당뇨병환자의 수가 적게 나타날까?
50대,60대의 수가 적어서 당뇨병 환자의 수가 적은 것이 아닐까?
우선 전체 인원수 중 50대의 60대의 수를 살펴보자
살펴보면 전체 인원수 중 50,60대의 수는 10%로 밖에 안되는걸 알 수있다.
그렇다면 나이대별 당뇨병 환자의 수를 비교해보자
# 연령별 구간 나누기
age_slice = list(range(20,100,10))
age_name = [str(x)+"대" for x in age_slice[:-1]]
# age_name
diab_copy = diab.copy()
diab_copy["age_slice"] = pd.cut(diab_copy["Age"],age_slice, labels=age_name)
diab_copy
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | age_slice | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6 | 148.0 | 72.0 | 35.0 | 125.5 | 33.6 | 0.627 | 50 | 1 | 40대 |
| 1 | 1 | 85.0 | 66.0 | 29.0 | 125.5 | 26.6 | 0.351 | 31 | 0 | 30대 |
| 2 | 8 | 183.0 | 64.0 | 29.0 | 125.5 | 23.3 | 0.672 | 32 | 1 | 30대 |
| 3 | 1 | 89.0 | 66.0 | 23.0 | 94.0 | 28.1 | 0.167 | 21 | 0 | 20대 |
| 4 | 0 | 137.0 | 40.0 | 35.0 | 168.0 | 43.1 | 2.288 | 33 | 1 | 30대 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 763 | 10 | 101.0 | 76.0 | 48.0 | 180.0 | 32.9 | 0.171 | 63 | 0 | 60대 |
| 764 | 2 | 122.0 | 70.0 | 27.0 | 125.5 | 36.8 | 0.340 | 27 | 0 | 20대 |
| 765 | 5 | 121.0 | 72.0 | 23.0 | 112.0 | 26.2 | 0.245 | 30 | 0 | 20대 |
| 766 | 1 | 126.0 | 60.0 | 29.0 | 125.5 | 30.1 | 0.349 | 47 | 1 | 40대 |
| 767 | 1 | 93.0 | 70.0 | 31.0 | 125.5 | 30.4 | 0.315 | 23 | 0 | 20대 |
724 rows × 10 columns
# diab_copy["age_slice"].value_counts()
age = diab_copy.pivot_table(index="Outcome",columns="age_slice",aggfunc="size")
# age.T
age.T.plot.bar(figsize=(15,7), rot=0)
<AxesSubplot:xlabel='age_slice'>
표본이 적은 60대를 제외하고 나이대가 높아지면 당뇨병에 걸릴 확률이 높다는걸 알 수 있다.
# 임신횟수
sns.countplot(data=diab, x="Pregnancies",hue="Outcome")
<AxesSubplot:xlabel='Pregnancies', ylabel='count'>
sns.barplot(data=diab, x="Outcome",y="Pregnancies")
<AxesSubplot:xlabel='Outcome', ylabel='Pregnancies'>
sns.boxplot(data=diab, x="Outcome",y="Pregnancies")
<AxesSubplot:xlabel='Outcome', ylabel='Pregnancies'>
# DiabetesPedigreeFunction (가족 이력)
sns.histplot(data=diab, x="DiabetesPedigreeFunction",hue="Outcome",alpha=0.6)
<AxesSubplot:xlabel='DiabetesPedigreeFunction', ylabel='Count'>
sns.barplot(data=diab, x="Outcome",y="DiabetesPedigreeFunction")
<AxesSubplot:xlabel='Outcome', ylabel='DiabetesPedigreeFunction'>
sns.boxplot(data=diab, x="Outcome",y="DiabetesPedigreeFunction")
<AxesSubplot:xlabel='Outcome', ylabel='DiabetesPedigreeFunction'>
# diab[["DiabetesPedigreeFunction","Outcome"]].corr()
임신횟수와 가족이력은 당뇨병과 약한 양의 상관관계를 갖는듯하다.
# 당뇨병과 상관관계가 높은 컬럼들
diab.corr()["Outcome"].sort_values(ascending=False).to_frame()
| Outcome | |
|---|---|
| Outcome | 1.000000 |
| Glucose | 0.488384 |
| BMI | 0.299375 |
| Age | 0.245741 |
| Pregnancies | 0.224417 |
| SkinThickness | 0.216357 |
| Insulin | 0.211736 |
| DiabetesPedigreeFunction | 0.184947 |
| BloodPressure | 0.166703 |
포도당 수치가 높고 BMI지수가 높으면 당뇨병에 걸릴 확률이 높다고 생각된다.
그러고 위의 히트맵으로 시각화했던 상관관계에서 포도당은 인슐린과, BMI는 피부두께와 상관관계가 높다는걸 알 수 있다.
sns.pairplot(diab[["Glucose","Insulin","Outcome"]],hue="Outcome")
<seaborn.axisgrid.PairGrid at 0x18b9127e2b0>
sns.pairplot(diab[["BMI","SkinThickness","Outcome"]],hue="Outcome")
<seaborn.axisgrid.PairGrid at 0x18b914574f0>
표준화 작업을 한 후
당뇨병인 사람들의 0과의 유클리디안 거리를 구해서 합친 후 사람수만큼 나눠주고
마찬가지로 정상인 사람들의 유클리디안 거리를 구해서 합친 후정상인수만큼 나눠준다.
테스트 데이터와 0과의 유클리디안 거리를 구해서 나온 값이 어디에 가까운지 판단하여 당뇨병인 사람들의 유클리디안
거리와 가장 가까운 10명을 구해보자
from sklearn.preprocessing import StandardScaler
from scipy.spatial import distance
# 표준화
diab_stand = pd.DataFrame(StandardScaler().fit_transform(diab.drop(["Outcome"],axis=1)),
columns=['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
'BMI', 'DiabetesPedigreeFunction', 'Age'])
diab_stand["Outcome"]= diab["Outcome"]
diab_stand
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.635022 | 0.849933 | -0.032378 | 0.650838 | -0.186619 | 0.164562 | 0.458421 | 1.416075 | 1.0 |
| 1 | -0.852861 | -1.200262 | -0.517370 | -0.014865 | -0.186619 | -0.852262 | -0.372691 | -0.199947 | 0.0 |
| 2 | 1.230175 | 1.988930 | -0.679035 | -0.014865 | -0.186619 | -1.331622 | 0.593928 | -0.114893 | 1.0 |
| 3 | -0.852861 | -1.070091 | -0.517370 | -0.680567 | -0.541941 | -0.634371 | -0.926765 | -1.050485 | 0.0 |
| 4 | -1.150438 | 0.491962 | -2.619006 | 0.650838 | 0.292783 | 1.544537 | 5.460146 | -0.029839 | 1.0 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 719 | 1.825328 | -0.679578 | 0.290951 | 2.093193 | 0.428143 | 0.062879 | -0.914720 | 2.521775 | 1.0 |
| 720 | -0.555284 | 0.003821 | -0.194042 | -0.236766 | -0.186619 | 0.629395 | -0.405815 | -0.540162 | 0.0 |
| 721 | 0.337445 | -0.028722 | -0.032378 | -0.680567 | -0.338900 | -0.910366 | -0.691886 | -0.285001 | 0.0 |
| 722 | -0.852861 | 0.133992 | -1.002363 | -0.014865 | -0.186619 | -0.343850 | -0.378713 | 1.160914 | 1.0 |
| 723 | -0.852861 | -0.939920 | -0.194042 | 0.207036 | -0.186619 | -0.300272 | -0.481097 | -0.880377 | 0.0 |
724 rows × 9 columns
# 당뇨병인 사람들의 표준화 데이터프레임
df_1 = diab_stand[diab_stand["Outcome"]==1].drop(["Outcome"],axis=1)
df_1
df_0 = diab_stand[diab_stand["Outcome"]==0].drop(["Outcome"],axis=1)
df_0
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| 1 | -0.852861 | -1.200262 | -0.517370 | -0.014865 | -0.186619 | -0.852262 | -0.372691 | -0.199947 |
| 3 | -0.852861 | -1.070091 | -0.517370 | -0.680567 | -0.541941 | -0.634371 | -0.926765 | -1.050485 |
| 5 | 0.337445 | -0.191436 | 0.129287 | -0.014865 | -0.186619 | -0.997523 | -0.824382 | -0.285001 |
| 10 | 1.825328 | 0.557048 | 0.614280 | -0.014865 | -0.186619 | -0.779632 | 2.909597 | 2.011452 |
| 12 | 0.337445 | 1.435703 | -0.032378 | -1.124369 | 0.371743 | -0.968471 | 0.337970 | 1.501129 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 717 | 1.527752 | 1.565874 | 0.129287 | 0.207036 | -0.186619 | 1.675271 | -0.216105 | 0.820699 |
| 718 | 1.527752 | -1.070091 | -0.840699 | -0.014865 | -0.186619 | -1.447830 | -1.002047 | -0.029839 |
| 720 | -0.555284 | 0.003821 | -0.194042 | -0.236766 | -0.186619 | 0.629395 | -0.405815 | -0.540162 |
| 721 | 0.337445 | -0.028722 | -0.032378 | -0.680567 | -0.338900 | -0.910366 | -0.691886 | -0.285001 |
| 723 | -0.852861 | -0.939920 | -0.194042 | 0.207036 | -0.186619 | -0.300272 | -0.481097 | -0.880377 |
448 rows × 8 columns
sum_df_1 = 0
for i in df_1.index:
sum_df_1 += distance.euclidean(df_1.loc[i],0)
sum_df_1 /= len(df_1.index)
sum_df_1
2.5492850340869255
sum_df_0 = 0
for i in df_0.index:
sum_df_0 += distance.euclidean(df_0.loc[i],0)
sum_df_0 /= len(df_0.index)
sum_df_0
2.6791406564496536
당뇨병 환자의 거리 평균은 2.5492850340869255
정상인의 거리 평균은 2.6791406564496536 이다.
이제 테스트 데이터를 표준화시키고 거리를 구해보자
test = pd.read_excel("test.xlsx", header=None)
test.columns=['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
test.head()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| 0 | 3 | 82 | 70 | 0 | 0 | 21.1 | 0.389 | 25 |
| 1 | 3 | 193 | 70 | 31 | 0 | 34.9 | 0.241 | 25 |
| 2 | 4 | 95 | 64 | 0 | 0 | 32.0 | 0.161 | 31 |
| 3 | 6 | 137 | 61 | 0 | 0 | 24.2 | 0.151 | 55 |
| 4 | 5 | 136 | 84 | 41 | 88 | 35.0 | 0.286 | 35 |
# test.isna().sum()
# 0값이 있는 컬럼들 찾기(결측치 확인)
missingValue=[]
for key in test.keys()[1:-1]:
if True in (test[key]==0).values:
missingValue.append(key)
missingValue
# 0인값 NaN으로 처리
for col in missingValue:
cnt=0
for j in test[col].values:
if j == 0:
test[col][cnt]=np.nan
cnt+=1
test.head()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| 0 | 3 | 82 | 70.0 | NaN | NaN | 21.1 | 0.389 | 25 |
| 1 | 3 | 193 | 70.0 | 31.0 | NaN | 34.9 | 0.241 | 25 |
| 2 | 4 | 95 | 64.0 | NaN | NaN | 32.0 | 0.161 | 31 |
| 3 | 6 | 137 | 61.0 | NaN | NaN | 24.2 | 0.151 | 55 |
| 4 | 5 | 136 | 84.0 | 41.0 | 88.0 | 35.0 | 0.286 | 35 |
test.fillna(test.median(),inplace=True)
test.head()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| 0 | 3 | 82 | 70.0 | 30.0 | 132.5 | 21.1 | 0.389 | 25 |
| 1 | 3 | 193 | 70.0 | 31.0 | 132.5 | 34.9 | 0.241 | 25 |
| 2 | 4 | 95 | 64.0 | 30.0 | 132.5 | 32.0 | 0.161 | 31 |
| 3 | 6 | 137 | 61.0 | 30.0 | 132.5 | 24.2 | 0.151 | 55 |
| 4 | 5 | 136 | 84.0 | 41.0 | 88.0 | 35.0 | 0.286 | 35 |
test_stand = pd.DataFrame(StandardScaler().fit_transform(test),
columns=['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
'BMI', 'DiabetesPedigreeFunction', 'Age'])
test_stand.head()
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0.058446 | -1.277157 | -0.326255 | 0.037663 | -0.166163 | -1.524059 | -0.268822 | -0.601683 |
| 1 | 0.058446 | 2.030459 | -0.326255 | 0.136777 | -0.166163 | 0.286237 | -0.654715 | -0.601683 |
| 2 | 0.423730 | -0.889779 | -0.930431 | 0.037663 | -0.166163 | -0.094188 | -0.863306 | 0.176355 |
| 3 | 1.154300 | 0.361752 | -1.232519 | 0.037663 | -0.166163 | -1.117399 | -0.889380 | 3.288507 |
| 4 | 0.789015 | 0.331954 | 1.083489 | 1.127919 | -0.651349 | 0.299355 | -0.537382 | 0.695047 |
(sum_df_1,sum_df_0)
(2.5492850340869255, 2.6791406564496536)
sum_df_1과 가장 거리가 까가운 순으로 정렬해보자
test_len = [] # 테스트 데이터의 유클리드 거리를 담아줄 리스트
for i in test_stand.index:
test_len.append(distance.euclidean(test_stand.loc[i],0))
test_stand["Length"] = test_len
test_stand["Outcome"] = np.where(abs(test_stand["Length"]-sum_df_0)>abs(test_stand["Length"]-sum_df_1),0,1)
test["Outcome"]=test_stand["Outcome"]
test
| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | Outcome | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 3 | 82 | 70.0 | 30.0 | 132.5 | 21.1 | 0.389 | 25 | 0 |
| 1 | 3 | 193 | 70.0 | 31.0 | 132.5 | 34.9 | 0.241 | 25 | 0 |
| 2 | 4 | 95 | 64.0 | 30.0 | 132.5 | 32.0 | 0.161 | 31 | 0 |
| 3 | 6 | 137 | 61.0 | 30.0 | 132.5 | 24.2 | 0.151 | 55 | 1 |
| 4 | 5 | 136 | 84.0 | 41.0 | 88.0 | 35.0 | 0.286 | 35 | 0 |
| 5 | 9 | 72 | 78.0 | 25.0 | 132.5 | 31.6 | 0.280 | 38 | 1 |
| 6 | 5 | 168 | 64.0 | 30.0 | 132.5 | 32.9 | 0.135 | 41 | 0 |
| 7 | 2 | 123 | 48.0 | 32.0 | 165.0 | 42.1 | 0.520 | 26 | 1 |
| 8 | 4 | 115 | 72.0 | 30.0 | 132.5 | 28.9 | 0.376 | 46 | 0 |
| 9 | 0 | 101 | 62.0 | 30.0 | 132.5 | 21.9 | 0.336 | 25 | 0 |
| 10 | 8 | 197 | 74.0 | 30.0 | 132.5 | 25.9 | 1.191 | 39 | 1 |
| 11 | 1 | 172 | 68.0 | 49.0 | 579.0 | 42.4 | 0.702 | 28 | 1 |
| 12 | 6 | 102 | 90.0 | 39.0 | 132.5 | 35.7 | 0.674 | 28 | 0 |
| 13 | 1 | 112 | 72.0 | 30.0 | 176.0 | 34.4 | 0.528 | 25 | 0 |
| 14 | 1 | 143 | 84.0 | 23.0 | 310.0 | 42.4 | 1.076 | 22 | 1 |
| 15 | 1 | 143 | 74.0 | 22.0 | 61.0 | 26.2 | 0.256 | 21 | 0 |
| 16 | 0 | 138 | 60.0 | 35.0 | 167.0 | 34.6 | 0.534 | 21 | 0 |
| 17 | 3 | 173 | 84.0 | 33.0 | 474.0 | 35.7 | 0.258 | 22 | 1 |
| 18 | 1 | 97 | 68.0 | 21.0 | 132.5 | 27.2 | 1.095 | 22 | 0 |
| 19 | 4 | 144 | 82.0 | 32.0 | 132.5 | 38.5 | 0.554 | 37 | 0 |
| 20 | 1 | 83 | 68.0 | 30.0 | 132.5 | 18.2 | 0.624 | 27 | 0 |
| 21 | 3 | 129 | 64.0 | 29.0 | 115.0 | 26.4 | 0.219 | 28 | 0 |
| 22 | 1 | 119 | 88.0 | 41.0 | 170.0 | 45.3 | 0.507 | 26 | 1 |
| 23 | 2 | 94 | 68.0 | 18.0 | 76.0 | 26.0 | 0.561 | 21 | 0 |
| 24 | 0 | 102 | 64.0 | 46.0 | 78.0 | 40.6 | 0.496 | 21 | 1 |
| 25 | 2 | 115 | 64.0 | 22.0 | 132.5 | 30.8 | 0.421 | 21 | 0 |
| 26 | 8 | 151 | 78.0 | 32.0 | 210.0 | 42.9 | 0.516 | 36 | 1 |
| 27 | 4 | 184 | 78.0 | 39.0 | 277.0 | 37.0 | 0.264 | 31 | 1 |
| 28 | 0 | 94 | 72.0 | 30.0 | 132.5 | 32.9 | 0.256 | 25 | 0 |
| 29 | 1 | 181 | 64.0 | 30.0 | 180.0 | 34.1 | 0.328 | 38 | 0 |
| 30 | 0 | 135 | 94.0 | 46.0 | 145.0 | 40.6 | 0.284 | 26 | 1 |
| 31 | 1 | 95 | 82.0 | 25.0 | 180.0 | 35.0 | 0.233 | 43 | 0 |
| 32 | 2 | 99 | 72.0 | 30.0 | 132.5 | 22.2 | 0.108 | 23 | 0 |
| 33 | 3 | 89 | 74.0 | 16.0 | 85.0 | 30.4 | 0.551 | 38 | 0 |
| 34 | 1 | 80 | 74.0 | 11.0 | 60.0 | 30.0 | 0.527 | 22 | 1 |
| 35 | 2 | 139 | 75.0 | 30.0 | 132.5 | 25.6 | 0.167 | 29 | 0 |
| 36 | 1 | 90 | 68.0 | 8.0 | 132.5 | 24.5 | 1.138 | 36 | 1 |
| 37 | 0 | 141 | 72.0 | 30.0 | 132.5 | 42.4 | 0.205 | 29 | 0 |
| 38 | 12 | 140 | 85.0 | 33.0 | 132.5 | 37.4 | 0.244 | 41 | 1 |
| 39 | 5 | 147 | 75.0 | 30.0 | 132.5 | 29.9 | 0.434 | 28 | 0 |
| 40 | 1 | 97 | 70.0 | 15.0 | 132.5 | 18.2 | 0.147 | 21 | 1 |
| 41 | 6 | 107 | 88.0 | 30.0 | 132.5 | 36.8 | 0.727 | 31 | 0 |
| 42 | 0 | 189 | 104.0 | 25.0 | 132.5 | 34.3 | 0.435 | 41 | 1 |
| 43 | 2 | 83 | 66.0 | 23.0 | 50.0 | 32.2 | 0.497 | 22 | 0 |
| 44 | 4 | 117 | 64.0 | 27.0 | 120.0 | 33.2 | 0.230 | 24 | 0 |
| 45 | 8 | 108 | 70.0 | 30.0 | 132.5 | 30.5 | 0.955 | 33 | 0 |
| 46 | 4 | 117 | 62.0 | 12.0 | 132.5 | 29.7 | 0.380 | 30 | 0 |
| 47 | 0 | 180 | 78.0 | 63.0 | 14.0 | 59.4 | 2.420 | 25 | 1 |
| 48 | 1 | 100 | 72.0 | 12.0 | 70.0 | 25.3 | 0.658 | 28 | 0 |
| 49 | 0 | 95 | 80.0 | 45.0 | 92.0 | 36.5 | 0.330 | 26 | 0 |